package com.greateamd.timetableasy.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import net.sf.gilead.pojo.gwt.LightEntity;

@SuppressWarnings("serial")
@Entity
@Table(name="cursuses")
public class Cursus extends LightEntity implements PersistableEntity,Serializable
{

	@Id
	@GeneratedValue
	private Integer id;
	
	@Column(name="name",length=55, nullable=false)
	private String name;

	@Column(name="enabled",nullable=false)
	private boolean enabled = true;

	@Column(name="start_date",nullable=false)
	private Date startDate;
	@Column(name="end_date",nullable=false)
	private Date endDate;
	
	@ManyToOne
	@JoinColumn(nullable=false,name="university")
	private University university;
	
	//TODO: Make StudyPeriod not optional
	@OneToMany(mappedBy="cursus", fetch=FetchType.EAGER, cascade=CascadeType.ALL)
	private Set<StudyPeriod> studyPeriods;
	
	public Cursus()
	{}

	public Cursus(String name, Date start,Date end,University university) 
	{
		this.name = name;
		this.startDate = start;
		this.endDate = end;
		this.university = university;
	}

	public Set<StudyPeriod> getStudyPeriods() {
		return studyPeriods;
	}

	public void setStudyPeriods(Set<StudyPeriod> studyPeriods) {
		this.studyPeriods = studyPeriods;
	}
	
	public void addStudyPeriod(StudyPeriod studyPeriod) {
		if (studyPeriods == null)
			studyPeriods = new HashSet<StudyPeriod>();
		this.studyPeriods.add(studyPeriod);
	}

	public Date getStartDate() {
		return startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public University getUniversity() {
		return university;
	}

	public void setUniversity(University university) {
		this.university = university;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() 
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public boolean isEnabled() 
	{
		return enabled;
	}

	public void setEnabled(boolean enabled)
	{
		this.enabled = enabled;
	}

	@Override
	public String toString() {
		return getName();
	}
	
//	public Set<Promotion> getPromotions() {
//		return promotions;
//	}
//
//	public void setPromotions(Set<Promotion> promotions) {
//		this.promotions = promotions;
//	}
//
//	public void addPromotion(Promotion promotion) {
//		if (promotions == null)
//			promotions = new HashSet<Promotion>();
//		this.promotions.add(promotion);
//	}
}
